local duofu = fk.CreateSkill{
  name = "hanqing__duofu",
  dynamic_desc = function(self, player)
    if player:getMark(self.name) ~= 0 then
      return "hanqing__duofu_inner:"..table.concat(table.map(player:getTableMark(self.name), function (s)
        return Fk:translate(s)
      end), "/")
    end
  end,
}

Fk:loadTranslationTable{
  ["hanqing__duofu"] = "夺符",
  [":hanqing__duofu"] = "出牌阶段，你可以重铸至多三张♠牌，视为使用一张<a href=':underhanding'>【瞒天过海】</a>。"..
  "若重铸牌均可使用，你回复1点体力。若均不可使用，结算后你选择一项：1.失去1点体力；2.不能再重铸此值张牌。",

  [":hanqing__duofu_inner"] = "出牌阶段，你可以重铸至多三张♠牌，视为使用一张<a href=':underhanding'>【瞒天过海】</a>。"..
  "若重铸牌均可使用，你回复1点体力。若均不可使用，结算后你选择一项：1.失去1点体力；2.不能再重铸此值张牌（已禁止重铸张数：{1}）。",

  ["#hanqing__duofu"] = "夺符：重铸至多三张♠牌，视为使用【瞒天过海】",
  ["hanqing__duofu_choice"] = "发动“夺符”不能再重铸%arg张牌",
}

duofu:addEffect("viewas", {
  anim_type = "control",
  prompt = "#hanqing__duofu",
  card_filter = function (self, player, to_select, selected)
    return #selected < 3 and Fk:getCardById(to_select).suit == Card.Spade
  end,
  view_as = function(self, player, cards)
    if #cards == 0 or table.contains(player:getTableMark(duofu.name), #cards) then return end
    local card = Fk:cloneCard("underhanding")
    card.skillName = duofu.name
    card:addFakeSubcards(cards)
    return card
  end,
  before_use = function (self, player, use)
    local room = player.room
    local cards = table.simpleClone(use.card.fake_subcards)
    use.extra_data = use.extra_data or {}
    local n = #table.filter(cards, function (id)
      return player:canUse(Fk:getCardById(id))
    end)
    if n == #cards then
      use.extra_data.hanqing__duofu_recover = true
    elseif n == 0 then
      use.extra_data.hanqing__duofu = #cards
    end
    room:recastCard(cards, player, duofu.name)
  end,
  after_use = function (self, player, use)
    local room = player.room
    if player.dead then return end
    if use.extra_data.hanqing__duofu_recover then
      if player:isWounded() then
        room:recover{
          who = player,
          num = 1,
          recoverBy = player,
          skillName = duofu.name,
        }
      end
    elseif use.extra_data.hanqing__duofu then
      local n = use.extra_data.hanqing__duofu
      local choice = room:askToChoice(player, {
        choices = {
          "loseHp",
          "hanqing__duofu_choice:::"..n,
        },
        skill_name = duofu.name,
      })
      if choice == "loseHp" then
        room:loseHp(player, 1, duofu.name)
      else
        room:addTableMark(player, duofu.name, n)
      end
    end
  end,
  enabled_at_play = function (self, player)
    return #player:getTableMark(duofu.name) < 3
  end,
})

duofu:addAI(nil, "vs_skill")

return duofu
